//
//  KFDEmoticonParser.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/21/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDEmoticonParser.h"

@implementation KFDEmoticon

@end

@implementation KFDEmoticonParser

+(KFDEmoticonParser*)parser
{
    KFDEmoticonParser* parser=[[KFDEmoticonParser alloc] init];
    return parser;
}

-(instancetype)init
{
    if (self=[super init]) {
        self.emoticons=[[NSMutableArray alloc] init];
    }
    return self;
}

-(NSString*)parseContent:(NSString *)content
{
    NSMutableString* mutableContent=content.mutableCopy;
    NSError* error;
    NSRegularExpression* regext=[NSRegularExpression regularExpressionWithPattern:@"emot-[0-9]+" options:0 error:&error];
    NSArray* results=[regext matchesInString:content options:0 range:NSMakeRange(0, content.length)];
    if (results) {
        for (NSTextCheckingResult* result in results) {
            NSRange range=[result range];
            NSString* match=[content substringWithRange:range];
            NSError* err;
            NSRegularExpression* reg=[NSRegularExpression regularExpressionWithPattern:@"[0-9]+" options:0 error:&err];
            NSTextCheckingResult* res=[reg firstMatchInString:match options:0 range:NSMakeRange(0, match.length)];
            NSString* num=[match substringWithRange:[res range]];
            NSMutableString* placeHolder=[NSMutableString string];
            for (int i=0; i<range.length; i++) {
                [placeHolder appendString:@" "];
            }
            [mutableContent replaceCharactersInRange:range withString:placeHolder];
            KFDEmoticon* emoticon=[[KFDEmoticon alloc] init];
            emoticon.range=range;
            emoticon.number=num.integerValue;
            [self.emoticons addObject:emoticon];
        }
    }
    return mutableContent;
}

@end
